package com.zhang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.mapper.IMainMapper;
import com.zhang.mapper.IReplyMapper;
import com.zhang.pojo.MainBean;
import com.zhang.pojo.ReplyBean;
import com.zhang.service.IMainService;
import jdk.tools.jaotc.Main;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class MainServiceImpl implements IMainService {

    @Autowired
    private IMainMapper mainMapper;
    @Autowired
    private IReplyMapper replyMapper;

    @Override
    public PageInfo<MainBean> findByChannelId(int pageNo, int channelId, String name, String author, LocalDate start, LocalDate end) {
        PageHelper.startPage(pageNo,3);
        PageInfo<MainBean> mainBeans = PageInfo.of(mainMapper.findByChannelId(channelId, name, author, start, end));
        return mainBeans;
    }

    @Override
    public void addMain(MainBean mainBean, int userId, int channelId) {
        mainMapper.addMain(mainBean, userId, channelId);
    }

    @Override
    public MainBean findById(int mainId) {
        MainBean mainBean = mainMapper.findById(mainId);
        List<ReplyBean> replyBeans = replyMapper.findByMainId(mainId);
        mainBean.setReplyBeans(replyBeans);
        return mainBean;
    }
}
